package com.eqdd.yiqidian.ui.gongneng.car;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;

import com.eqdd.yiqidian.R;
import com.eqdd.yiqidian.RecyclerViewRefreshCustom;
import com.eqdd.yiqidian.adapter.slimadapter.SlimAdapterEx;
import com.eqdd.yiqidian.base.AppConstent;
import com.eqdd.yiqidian.base.BaseActivity;
import com.eqdd.yiqidian.base.Config;
import com.eqdd.yiqidian.bean.rebuild.CarAccidentListBean;
import com.eqdd.yiqidian.bean.rebuild.CarAccidentListBean;
import com.eqdd.yiqidian.bean.rebuild.ZhangPageResultBean;
import com.eqdd.yiqidian.helper.mvchelper.ModelRx2DataSource;
import com.eqdd.yiqidian.helper.mvchelper.Rx2DataSource;
import com.eqdd.yiqidian.http.HttpConfig;
import com.eqdd.yiqidian.http.JsonConverter;
import com.eqdd.yiqidian.utils.SlimUtil;
import com.eqdd.yiqidian.utils.ToastUtil;
import com.lzy.okgo.OkGo;
import com.lzy.okrx2.adapter.FlowableBody;
import com.shizhefei.mvc.MVCCoolHelper;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.Flowable;

/**
 * @author吕志豪 .
 * @date 17-11-20  上午11:17.
 * Github :https://github.com/lvzhihao100
 * E-Mail：1030753080@qq.com
 * 简书 :http://www.jianshu.com/u/6e525b929aac
 */

public class CarAccidentListActivity extends BaseActivity {

    private RecyclerViewRefreshCustom dataBinding;
    private SlimAdapterEx<CarAccidentListBean> slimAdapterEx;
    private int pageNum = 0;
    private MVCCoolHelper<List<CarAccidentListBean>> mvcHelper;

    @Override
    public void initBinding() {
        dataBinding = DataBindingUtil.setContentView(this, R.layout.activity_recyclerview_refresh);
        initTopTitleBar(View.VISIBLE, "车辆事故列表");
        initTopRightText("添加", v ->
                startActivityForResult(AddAccidentActivity.class, null, Config.ADD_CAR_ACCIDENT));
    }

    @Override
    public void initData() {
        dataBinding.recyclerView.setLayoutManager(new LinearLayoutManager(this));
        slimAdapterEx = SlimAdapterEx.create().attachTo(dataBinding.recyclerView).updateData(new ArrayList());
        SlimUtil.layout(slimAdapterEx, R.layout.list_item_12, CarAccidentListBean.class);
        ModelRx2DataSource<CarAccidentListBean> dataSource = new ModelRx2DataSource<>(new ModelRx2DataSource.OnLoadSource() {
            @Override
            public Flowable<List> loadSource(int page, Rx2DataSource.DoneActionRegister register) {
                pageNum = page == 1 ? 0 : pageNum;
                return OkGo.<ZhangPageResultBean<List<CarAccidentListBean>>>post(HttpConfig.BASE_URL + HttpConfig.CAR_ACCIDENT_LIST)
                        .params("page", pageNum)
                        .params("comid", AppConstent.user.getCompanyId())
                        .params("userGuid", AppConstent.user.getGuid())
                        .params("vehicleId", 0)
                        .converter(new JsonConverter<ZhangPageResultBean<List<CarAccidentListBean>>>() {
                            @Override
                            public void test() {
                                super.test();
                            }
                        }).adapt(new FlowableBody<>())
                        .flatMap(listResultBean -> {
                            pageNum =  listResultBean.getNextpage();
                            if (listResultBean.getStatus() == 200) {
                                return Flowable.just(listResultBean.getItems() == null ? new ArrayList<CarAccidentListBean>() : listResultBean.getItems());
                            } else {
                                ToastUtil.showShort(listResultBean.getMsg());
                                return Flowable.just(new ArrayList<>());
                            }
                        });

            }
        });
        slimAdapterEx.updateData(new ArrayList<>());
        mvcHelper = new MVCCoolHelper<>(dataBinding.coolRefreshView);
        mvcHelper.setDataSource(dataSource);
        mvcHelper.setAdapter(slimAdapterEx);
        mvcHelper.refresh();
        dataBinding.recyclerView.setSwipeItemClickListener((view, position) -> {
            Bundle bundle = new Bundle();
            bundle.putInt(Config.ID, slimAdapterEx.getDataItem(position).getVehicleId());
            startActivity(CarAccidentInfoActivity.class, bundle);
        });
    }

    @Override
    public void setView() {

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Config.SUCCESS && requestCode == Config.ADD_CAR_ACCIDENT) {
            mvcHelper.refresh();
        }
    }
}
